<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<jsp:include page="../parts/adminFooter.jsp"></jsp:include>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>菜单首页</title>
</head>
<body>
<script type="text/javascript">
	$(function(){
		$("#dg").treegrid({
			fit:true,
			url:'${ctx}/menu/find',
			treeField:'text',//列存放树的折叠
			idField:'id',//行数据的唯一
			parentField:'parentId',//父id
			rownumbers:true,
			singleSelect:true,
			columns:[[{
				field:'text',
				title:'菜单名称',
				width:'15%'
			},{
				field:'parentText',
				title:'父菜单名称',
				width:'15%'
			},{
				field:'url',
				title:'链接',
				width:'15%'
			},{
				field:'icon',
				title:'图标',
				width:'15%'
			},{
				field:'sort',
				title:'顺序',
				width:'15%'
			},{
				field:'createDate',
				title:'创建时间',
				width:150
			}]],
			toolbar:[{
				text:'新&nbsp;&nbsp;增',
				iconCls:'icon-eadd',
				width:80,
				height:30,
				handler:function(){
					openAddDia('新增菜单',null);
				}
			},'-',{
				text:'修&nbsp;&nbsp;改',
				iconCls:'icon-eedit',
				width:80,
				height:30,
				handler:function(){
					var row = $("#dg").datagrid('getSelected');
					if(row==null){
						alert("请选择要修改的记录");
						return;
					}
					//把对象转换成字符串
					//row = JSON.stringify(row);
					//移除属性
					delete row.createDate;
					openAddDia('修改菜单',row);
				}
			},'-',{
				text:'删&nbsp;&nbsp;除',
				iconCls:'icon-remove',
				width:80,
				height:30,
				handler:function(){
					deleteRec();
				}
			}]
		});
	});
	function deleteRec(){
		var row = $('#dg').datagrid('getSelected');
		if(row==null){
			alert("请选择要删除的记录");
			return;
		}
		$.messager.confirm('确认删除',
		'真的删除该记录(子目录一起删除！)',
		function(r){
			if(r){
				$.ajax({
					type:'post',
					url:'${ctx}/menu/delete/'+row.id,
					dataType:'json',
					success:function(data){
						alert(data.msg);
						if(data.sus){
							//表格自动刷新
							$('#dg').treegrid('reload');
						}
					}
				});
			}
		});
	}
	
	function openAddDia(title,js){
		parent.$.modalDialog({
			title:title,
			//href:'${ctx}/menu/aoe?js='+js,
			href:'${ctx}/menu/aoe',
			queryParams:js,
			cache:false,
			closable:false,
			width:350,
			height:350,
			buttons:[{
				text:'保&nbsp;&nbsp;存',
				iconCls:'icon-save',
				width:80,
				height:30,
				handler:function(){	
				    var url = '${ctx}/menu/insert';
					if(js!=null){
						url = '${ctx}/menu/update';
					}
					saveUser(url);
				}
			},{
				text:'取&nbsp;&nbsp;消',
				iconCls:'icon-cancel',
				width:80,
				height:30,
				handler:function(){
					//全屏对话框的关闭
					parent.$.modalDialog.handler.dialog('close');
				}
			}]
		});
	
	}
	function saveUser(url){
		var f= parent.$.modalDialog.handler.find('#menuForm');
		if(f.form('validate')){
			var value= f.serialize();
			$.ajax({
				type:'post',
				url:url,
				data:value,
				dataType:'json',
				success:function(data){
					alert(data.msg);
					if(data.sus){
						//全屏对话框的关闭
						parent.$.modalDialog.handler.dialog('close');
						//表格自动刷新
						$('#dg').treegrid('reload');
					}
				}
			});
		}
	
	}
</script>
<table id="dg"></table>
</body>
</html>